package com.siwei.mes.common.util;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 转换工具类
 *
 * @Description: 转换工具类
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/10/16 10:12
 */
public class ConvertUtils {
    /**
     * 转换砼品种  比如
     * 水下C40P8   变成  水下C40
     * C40补偿收缩  变成  C40
     * 耐久性C35   变成  C35
     *
     * @param str
     * @return
     */
    public static String getMesTpz(String str) {
        try {
            str = str.trim().toUpperCase();
            Pattern pattern = Pattern.compile("C(\\d+\\.?\\d*)");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                if (str.contains("水下C")) {
                    return "水下C" + matcher.group(1);
                } else {
                    return "C" + matcher.group(1);
                }
            }
            return str;
        } catch (Exception e) {
            return str;
        }
    }


    /**
     * 获取对应抗渗等级
     * 耐久性C35P8HEA聚丙烯纤维  变成P8
     *
     * @param str
     * @return
     */
    public static String getMesTpzToKs(String str) {
        try {
            str = str.trim().toUpperCase();
            Pattern pattern = Pattern.compile("P(\\d+\\.?\\d*)");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return "P" + matcher.group(1);
            }
            return "";
        } catch (Exception e) {
            return str;
        }
    }

    /**
     * 获取对应抗折要求
     * C30Z4.0  变成4.0
     *
     * @param str
     * @return
     */
    public static String getMesTpzToKz(String str) {
        try {
            str = str.trim().toUpperCase();
            Pattern pattern = Pattern.compile("Z(\\d+\\.?\\d*)");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return "Z" + matcher.group(1);
            }
            return "";
        } catch (Exception e) {
            return str;
        }
    }

    /**
     * 获取对应抗折要求
     * C30Z4.0  变成4.0
     *
     * @param str
     * @return
     */
    public static String getMesTpzToKzyq(String str) {
        try {
            str = str.trim().toUpperCase();
            Pattern pattern = Pattern.compile("Z(\\d+\\.?\\d*)");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return matcher.group(1);
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取对应抗氯离子
     *
     * @param str
     * @return
     */
    public static String getMesTpzToKllz(String str) {
        try {
            str = str.trim().toUpperCase();
            Pattern pattern = Pattern.compile("CL(\\d+\\.?\\d*)");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return "CL" + matcher.group(1);
            }
            return "";
        } catch (Exception e) {
            return str;
        }
    }

    /**
     * 将对象中的空值转换为null
     *
     * @param obj
     * @return
     */
    public static void setFieldsToNullIfEmpty(Object obj) {
        if (obj == null) {
            return;
        }

        Class<?> objClass = obj.getClass();
        for (Field field : objClass.getDeclaredFields()) {
            field.setAccessible(true); // 确保私有属性也可以访问
            try {
                Object value = field.get(obj);
                if (value instanceof String && ((String) value).isEmpty()) {
                    field.set(obj, null); // 空字符串设置为null
                } else if (value instanceof Collection && ((Collection<?>) value).isEmpty()) {
                    field.set(obj, null); // 空集合设置为null
                } else if (value instanceof Map && ((Map<?, ?>) value).isEmpty()) {
                    field.set(obj, null); // 空Map设置为null
                } else if (value != null && !field.getType().isPrimitive()) {
                    // 对非空且非原始类型字段递归调用
                    setFieldsToNullIfEmpty(value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取抗氯离子含量
     *
     * @param kllz 抗氯离子
     * @return
     */
    public static String getKllzhl(String kllz) {
        try {
            if (StringUtils.isBlank(kllz)) {
                return null;
            }
            kllz = kllz.trim().toUpperCase();
            return kllz.replace("CL", "");
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取塌落度 拼接mm 单位
     *
     * @param str 原始塌落度
     * @return
     */
    public static String getTldUnit(String str) {
        String tld = "";
        if (StringUtils.isBlank(str)) {
            return tld;
        }
        if (str.contains("±")) {
            String[] split = str.split("±");
            if (split[0] != null) {
                tld = split[0] + "mm";
            }
            if (split[1] != null) {
                tld = tld + "±" + split[1] + "mm";
            }
        } else {
            tld = str + "mm";
        }
        return tld;
    }

    public static void main(String[] args) {
        String str1 = "C30Z4.0";
        //String str1 = "C30";
        String mesTpzToKz = getMesTpzToKz(str1);
        System.out.println(mesTpzToKz);

        String str = "C30CL3.1Z4.0";
        String mesTpzToKllz = getMesTpzToKllz(str);
        System.out.println(mesTpzToKllz);

        String str2 = "耐久性C35P8HEA聚丙烯纤维";
        String mesTpzToKs = getMesTpzToKs(str2);
        System.out.println(mesTpzToKs);

        System.out.println(getMesTpz("C35HEA抗裂纤维"));

        System.out.println(getTldUnit("180±20"));
        System.out.println(getTldUnit("≤700"));

        System.out.println(getMesTpzToKs("C35P8"));

    }
}